package com.example.yiqinmanagersystem.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yiqinmanagersystem.entity.Vaccine;
import com.example.yiqinmanagersystem.entity.Vaccine;
import com.example.yiqinmanagersystem.service.VaccineService;
import com.example.yiqinmanagersystem.service.VaccineService;
import com.example.yiqinmanagersystem.vo.VaccineVo;
import com.example.yiqinmanagersystem.vo.ResponseCode;
import com.example.yiqinmanagersystem.vo.ResponseResult;
import com.example.yiqinmanagersystem.vo.VaccineVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
 **@Author:youpeng
 **Created on 2022/9/18
 */
@RestController
@RequestMapping("/api")
public class VaccineController {

    @Resource
    private VaccineService vaccineService;

    /*
    * 获取核酸数据 带条件查询
    * */
    @GetMapping("/vaccine/loadAllVaccine")
    public ResponseResult loadAllVaccine(VaccineVo vaccineVo){
        //1.条件
        IPage<Vaccine> page = new Page<>(vaccineVo.getPage(),vaccineVo.getLimit());
        QueryWrapper<Vaccine> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(vaccineVo.getName()),"name",vaccineVo.getName());
        //2.查库
        IPage<Vaccine> vaccineIPage = vaccineService.page(page, queryWrapper);
        if(vaccineIPage!=null){
            return new ResponseResult(ResponseCode.SUCCESS, Math.toIntExact(vaccineIPage.getTotal()),vaccineIPage.getRecords());
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 删除核酸数据 id
    * */
    @PostMapping("/vaccine/deleteVaccine")
    public ResponseResult deleteVaccine(Integer id){
        boolean b = vaccineService.removeById(id);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 新增核酸数据
    * */
    @PostMapping("/vaccine/addVaccine")
    public ResponseResult addVaccine(Vaccine Vaccine){
        boolean b = vaccineService.save(Vaccine);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
     * 修改核酸数据
     * */
    @PostMapping("/vaccine/updateVaccine")
    public ResponseResult updateVaccine(Vaccine Vaccine){
        boolean b = vaccineService.updateById(Vaccine);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }
}
